import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../app_files.dart';
import 'action.dart';
import 'state.dart';

Widget buildView(
    OrderListViewState state, Dispatch dispatch, ViewService viewService) {
  Widget _buildItem(BuildContext context, int index) {
    OrderListItemModel item = state.items[index];
    Widget orderStatusText() {
      if (item.status == '2')
        return Container(
          decoration: BoxDecoration(
            border: Border.all(
              color: Color(0xff20E188),
            ),
            borderRadius: BorderRadius.all(Radius.circular(6)),
          ),
          padding: EdgeInsets.only(left: 4, right: 4, bottom: 1.5),
          child: Text(
            '已圈存',
            style: TextStyle(color: Color(0xff20E188), fontSize: 11),
          ),
        );

      if (item.status == '1') {
        if (item.isSupportSave)
          return Container(
            decoration: BoxDecoration(
              border: Border.all(
                color: Color(0xffFC756A),
              ),
              borderRadius: BorderRadius.all(Radius.circular(6)),
            ),
            padding: EdgeInsets.only(left: 4, right: 4, bottom: 1.5),
            child: Text(
              '待圈存',
              style: TextStyle(color: Color(0xffFC756A), fontSize: 11),
            ),
          );
        return Container(
          decoration: BoxDecoration(
            border: Border.all(
              color: ThemeConfig.mainColor,
            ),
            borderRadius: BorderRadius.all(Radius.circular(6)),
          ),
          padding: EdgeInsets.only(left: 4, right: 4, bottom: 1.5),
          child: Text(
            '已支付',
            style: TextStyle(color: ThemeConfig.mainColor, fontSize: 11),
          ),
        );
      }
      return Container(
        decoration: BoxDecoration(
          border: Border.all(
            color: Color(0xffFEC162),
          ),
          borderRadius: BorderRadius.all(Radius.circular(6)),
        ),
        padding: EdgeInsets.only(left: 4, right: 4, bottom: 1.5),
        child: Text(
          '未支付',
          style: TextStyle(color: Color(0xffFEC162), fontSize: 11),
        ),
      );
    }

    return InkWell(
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.only(left: 15, right: 15),
        child: Row(
          children: [
            Expanded(
              child: Container(
                margin: EdgeInsets.only(top: 10),
                decoration: BoxDecoration(
                  border: Border(
                      bottom: BorderSide(color: ThemeConfig.separeColor)),
                ),
                child: Row(
                  children: [
                    Column(
                      children: [
                        Row(
                          children: [
                            Text(
                              item.name,
                              style: TextThemeConfig.Font15WithColor22,
                            ),
                            Padding(padding: EdgeInsets.only(left: 12)),
                            orderStatusText(),
                          ],
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 3),
                          child: Text(
                            item.accountNo,
                            style: TextThemeConfig.Font13WithColor99,
                          ),
                        ),
                      ],
                      crossAxisAlignment: CrossAxisAlignment.start,
                    ),
                    Expanded(child: Container()),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text(
                          '¥${item.money}',
                          style: TextThemeConfig.Font18WithBoldColor22,
                        ),
                        Text(
                          item.payTime,
                          style: TextThemeConfig.Font12WithColor99,
                        )
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
      onTap: () => CommonUtils.pushRoute(
          viewService.context, RoutePath.orderDetailView,
          arguments: {'model': item}),
    );
  }

  Widget _emptyWidget() => MyEmptyItemWidget(
        image: 'images/no_message.png',
        desc: '暂无订单...',
        padding: 130,
      );

  return Scaffold(
    appBar: myAppBar(
      viewService.context,
      title: '购气账单',
      actions: [
        MyInkWellWidget(
          child: Container(
            padding: EdgeInsets.only(right: 12),
            height: 40,
            child: Center(
              child: Text(
                '扣费记录',
                style: TextThemeConfig.ActionBarTitle,
              ),
            ),
          ),
          onTap: () => CommonUtils.pushRoute(
              viewService.context, RoutePath.gasUserAccountView,
              arguments: {'type': 4}),
        )
      ],
    ),
    backgroundColor: Colors.white,
    body: Column(
      children: [
        Container(
            padding: EdgeInsets.only(left: 15),
            height: 50,
            color: Color(0xffF8F8F8),
            child: InkWell(
              child: Row(
                children: [
                  Text(
                    state.selectTime == null
                        ? '请选择月份'
                        : '${state.selectTime.year}-${state.selectTime.month}',
                    style: TextThemeConfig.Font14WithColor22,
                  ),
                  Padding(padding: EdgeInsets.only(left: 5)),
                  Image.asset('images/ic_down.png'),
                ],
              ),
              onTap: () => dispatch(OrderListViewActionCreator.onTapTime()),
            )),
        Expanded(
          child: state.items == null
              ? MyLoadingWidget()
              : CustomRefreshList(
                  onRefresh: (String tag) =>
                      dispatch(OrderListViewActionCreator.onLoad(1)),
                  onLoading: (String tag, int page) =>
                      dispatch(OrderListViewActionCreator.onLoad(page)),
                  currentPage: state.currentPage,
                  refreshController: state.refreshController,
                  child: state.items.length == 0
                      ? _emptyWidget()
                      : ListView.builder(
                          itemBuilder: _buildItem,
                          itemCount: state.items.length,
                          itemExtent: 65,
                        ),
                ),
        )
      ],
    ),
  );
}
